import com.alibaba.fastjson.JSONObject;
import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;
import wei.yigulu.iec104.nettyconfig.Iec104SlaverBuilder;
import wei.yigulu.iec104.util.SendDataFrameHelper;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;

@Slf4j
public class StartHttpServer {
    public static void main(String[] args) throws Exception {
        //104端口
        Integer port1 = 2404;
        //页面端口
        Integer port2 = 2405;
        Integer tem1;
        Integer tem2;
        File file = null;
        if (args.length == 1 || args.length > 3) {
            throw new RuntimeException("请输入两个端口，参数1为104Slver端口，参数2为配置页面端口,[参数3为配置文件路径]");
        }
        if (args.length == 2 || args.length == 3) {
            try {
                tem1 = Integer.parseInt(args[0]);
                if (tem1 > 65535 || tem1 < 0) {
                    throw new RuntimeException("参数1应为0-65535整数");
                }
            } catch (Exception e) {
                throw new RuntimeException("参数1应为0-65535整数");
            }
            try {
                tem2 = Integer.parseInt(args[1]);
                if (tem1 > 65535 || tem1 < 0) {
                    throw new RuntimeException("参数2应为0-65535整数");
                }
            } catch (Exception e) {
                throw new RuntimeException("参数2应为0-65535整数");
            }
            if (tem1.intValue() == tem2.intValue()) {
                throw new RuntimeException("参数1与参数2不能相同");
            }
            if (args.length == 3) {
                try {
                    String fileUrl = args[2];
                    File file1 = new File(fileUrl);
                    if (file1.exists() && file1.isFile()) {
                        file = file1;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    throw new RuntimeException("配置文件读取失败");
                }
            }
            port1 = tem1;
            port2 = tem2;
        }
        Iec104SlaverBuilder iec104SlaverBuilder = new Iec104SlaverBuilder(port1);
       /* iec104SlaverBuilder.getConnectFilterManager().appendFilter((c)-> {
            if (iec104SlaverBuilder.getChannels().size() >= 1) {
                log.warn("已有一个主站连接，拒绝新连接联入");
                return -1;
            }
            return 1;
        });*/
        iec104SlaverBuilder.createByUnBlock();
        RecurringTaskContainer.getInstance().addRecurringTask(60, "向对端发送突发上送", () -> {
            if (iec104SlaverBuilder.getChannels().size() > 0) {
                for (Channel c : iec104SlaverBuilder.getChannels()) {
                    SendDataFrameHelper.sendYxDataFrameDiscontinuity(c, ProtocolDataContainer.getInstance().getBooleans(), 1, 3, iec104SlaverBuilder.getLog());
                    SendDataFrameHelper.sendYcDataFrameDiscontinuity(c, ProtocolDataContainer.getInstance().getNumbers(), 1, 3, iec104SlaverBuilder.getLog());
                }
            }
            return null;
        });
        if (file != null) {
            FileInputStream is = null;
            StringBuilder stringBuilder = null;
            try {
                if (file.length() != 0) {
                    /**
                     * 文件有内容才去读文件
                     */
                    is = new FileInputStream(file);
                    InputStreamReader streamReader = new InputStreamReader(is);
                    BufferedReader reader = new BufferedReader(streamReader);
                    String line;
                    stringBuilder = new StringBuilder();
                    while ((line = reader.readLine()) != null) {
                        stringBuilder.append(line);
                    }
                    reader.close();
                    is.close();
                    RequestController.getInstance().batchImport(JSONObject.parseObject(stringBuilder.toString()));
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        HttpServer httpServer = new HttpServer(port2);
        httpServer.start();
    }

}
